package com.bkfm.framework.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.yak.core.utils.ConfigUtils;

/**
 * 
 *
 * @平台：统一开发平台
 * @文件名称：ExcelUtils.java
 * @文件描述：Excel导入导出工具
 * @author xuqiang
 * @日期：2018年9月10日上午10:27:18
 *
 */
public class ExcelUtils {

	
	/**
	 * 
	 * @方法没描述：设置单元格字都穿数值
	 * @author xq
	 * @param cell
	 * @param value
	 */
	public static void setCellStringValue(HSSFCell cell,Object value){
		if(value == null || value.equals("null") || value.equals("")){
			cell.setCellValue("");
		}else{
    		cell.setCellValue(String.valueOf(value));
		}
	}
	
	/**
	 * 
	 * @方法没描述：设置数值
	 * @author xq
	 * @param cell
	 * @param value
	 */
	public static void setCellBigDecimalValue(HSSFCell cell,BigDecimal value){
		double valueInfo = value.doubleValue();
		setCellNumberValue(cell,valueInfo);
	}
	
	/**
	 * 
	 * @方法没描述：设置数值
	 * @author xq
	 * @param cell
	 * @param value
	 */
	public static void setCellNumberValue(HSSFCell cell,double value){
    	cell.setCellValue(value);
	}
	
	/**
	 * 导出Excel文件
	 * @param filePath
	 * @param fieldList
	 * @param valueList
	 * @return
	 * @throws Exception
	 */
	public static String exportExcelFile(String filePath,List<BkfmExpImpColumn> fieldList,List<Map<String,Object>> valueList)throws Exception{
		if(StringUtils.isEmpty(filePath)) {
			//文件路径没有指定，直接系统自动生成下载文件路径
			filePath = ConfigUtils.getConfigValue("exp_file_dir")+"/"+System.currentTimeMillis()+".xls";
		}
		//读取excel模板
        HSSFWorkbook wb = new HSSFWorkbook();
        //读取了模板内所有sheet内容
        HSSFSheet sheet = wb.createSheet();
		//插入表头
        //int startRow = 0;
        HSSFRow headRow = sheet.createRow(0);
        for(int col = 0;col<fieldList.size();col++) {
        	BkfmExpImpColumn headerField = fieldList.get(col);
        	HSSFCell cell = headRow.createCell(col);
        	setCellStringValue(cell,headerField.getField_label());
        }
        if(valueList!=null && !valueList.isEmpty()) {
	        //导出数据
	        for(int rowIndex=0;rowIndex<valueList.size();rowIndex++) {
	        	HSSFRow row = sheet.createRow(rowIndex+1);
	        	Map<String,Object> valueMap = valueList.get(rowIndex);
	        	//循环列
	        	for(int col = 0;col<fieldList.size();col++) {
	            	BkfmExpImpColumn headerField = fieldList.get(col);
	            	HSSFCell cell = row.createCell(col);
	            	Object value = valueMap.get(headerField.getField_name());
	            	String className = (value==null?"":value.getClass().getName());
	        		if(className.equals("java.lang.Integer")){
	        			setCellNumberValue(cell,Double.parseDouble(String.valueOf(value)));
	        		}else if(className.equals("java.math.BigDecimal")){
	        			setCellBigDecimalValue(cell,(BigDecimal)value);
	        		}else if(className.equals("java.lang.Double")){
	        			setCellNumberValue(cell,(double)value);
	        		}else{
	        			setCellStringValue(cell,value);
	        		}
	            	//setCellStringValue(cell,valueMap.get(headerField.getH_field_us()));
	            }
	        }
        }
        
        File outputFile = new File(filePath);
        if(!outputFile.getParentFile().exists()){
        	outputFile.getParentFile().mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath);
        wb.write(out);
        wb.close();
        out.close();
		return filePath;
	}

}
